/**
 * @author 徐楠
 * @date 2021/12/18 11:51
 * @version 1.0
 */

package com.xunan.likou;


public class MaximumProductOfThreeNumbers {
    public static void main(String[] args) {

        int[] numbers = {1, 3, 2, 5};
        for (int number : numbers) {
            System.out.println(number);
        }
        int i = maximumProduct(numbers);
        for (int number : numbers) {
            System.out.println(number);
        }

        System.out.println();
        System.out.println(i);
        System.out.println(Integer.MAX_VALUE);
        System.out.println(-Integer.MAX_VALUE);
    }

    public static int maximumProduct(int[] nums) {

        int max1 = -Integer.MAX_VALUE;
        int max2 = -Integer.MAX_VALUE;
        int max3 = -Integer.MAX_VALUE;
        int min1 = Integer.MAX_VALUE;
        int min2 = Integer.MAX_VALUE;

        if (nums.length == 3){
            return nums[0]*nums[1]*nums[2];
        }
        for (int i = 0; i < nums.length; i++) {
            if (nums[i] <= min2 && nums[i] < min1) {
                min2 = min1;
                min1 = nums[i];
            } else if (nums[i] >= min1 && nums[i] < min2) {
                min2 = nums[i];
            }
            if (nums[i] > max1 && nums[i] >= max2 && nums[i] >= max3) {
                max3 = max2;
                max2 = max1;
                max1 = nums[i];
            } else if (nums[i] <= max1 && nums[i] > max2 && nums[i] >= max3) {
                max3 = max2;
                max2 = nums[i];
            } else if (nums[i] <= max1 && nums[i] <= max2 && nums[i] > max3) {
                max3 = nums[i];
            }

        }

        return Math.max(max1 * max2 * max3, min1 * min2 * max1);
    }
}
